package com.fcl.blog.exception;

import cn.hutool.core.util.StrUtil;
import com.fcl.blog.comment.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author 付
 * @Date 2023/10/11 10:17
 * @Version 1.0
 * @description  全局异常处理
 * @since 1.0
 */
@Slf4j
@RestControllerAdvice
public class ExceptionHandle {

    @ExceptionHandler(value= Exception.class)
    public Result excptionHandler(Exception e){
        log.error("系统错误"+e);
        return Result.error("系统错误:",e);
    }

    @ExceptionHandler(value = NullPointerException.class)
    public Result nullPointerHandler(NullPointerException nullPointerException){
        log.error("空指针异常"+nullPointerException);
        return Result.error("抱歉，内容未查询到或不存在:",nullPointerException);
    }


    //    暴露自定义异常
    @ExceptionHandler(value = ServiceException.class)
    public Result serviceExceptionHandler(ServiceException e){
        log.error("出错了"+e);
        String code = e.getCode();
        if (StrUtil.isBlank(code)) {
            return Result.error(code,e.getMessage());
        }
        return Result.error("抱歉，内容未查询到或不存在:",e);
    }
}
